// 验证码接口
const {
    str_status,
    pro
} = require('../common/pro_mysql');
const {
    createToken
} = require('../common/jwt_token');
const axios = require('axios')
// 生成6位数验证码
function createCode(){
    let arr = [0,1,2,3,4,5,6,7,8,9];
    let str = '';
    for (let index = 0; index < 6; index++) {
        str += arr[Math.floor(Math.random()*arr.length)]
    }
    return str
}

//生成36位smsKey
function createkey(){
    let arr = [
        0,1,2,3,4,5,6,7,8,9,'a','b','c','d','e','f','g','h',
        'i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
        'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O',
        'P','Q','R','S','T','U','V','W','X','Y','Z'
    ]
    let str = '';
    for (let index = 0; index < 36; index++) {
        str += arr[Math.floor(Math.random()*arr.length)]
    }
    return str
}

// 发送验证码接口
async function SendCodeMsg(req,res){
    let {loginPhone} = req.query;
    if(!/^(?:(?:\+|00)86)?1[3-9]\d{9}$/.test(loginPhone)){
        res.send(str_status(0,'手机号不合法')) 
    }else{
        let code = createCode();
        let key = createkey();
        axios.get(`https://fc-mp-f336d118-c712-43cc-a4c3-5eee1cdad894.next.bspapp.com/getCode?loginPhone=${loginPhone}&code=${code}`)
        .then(async result => {
            if(result.data.code == 1){
                let sql = `insert into sendcode (codes,keyes,createTime,loginPhone) values ("${code}","${key}","${Date.now()}","${loginPhone}")`
                let resultes = await pro(sql);
                if(resultes.code == 1){
                    res.send(str_status(1,'验证码发送成功',key))
                }else{
                    res.send(str_status(1,'验证码发送失败'))
                }
                    
            }else{
                res.send(str_status(0,'验证码发送失败'))  
            }   
        })
    }
}

// 登录验证码接口
async function VerifyCodeMsg(req,res){
    let {loginPhone,key,code} = req.query;
    if(!loginPhone || !key || !code){
        res.send(str_status(0,'参数错误')) 
    }else{
        let sql = `select * from sendcode where keyes="${key}" and codes="${code}" and loginPhone="${loginPhone}" and statues=0`
        let result = await pro(sql);
        if(result.code == 1 && result.result.length == 1){
            sql = `update sendcode set statues=1 where keyes="${key}" and codes="${code}" and loginPhone="${loginPhone}"`
            result = await pro(sql);
            if(result.code == 1){
                sql = `select * from user where loginPhone="${loginPhone}"`
                result = await pro(sql);
                if(result.code == 1 && result.result.length == 1){
                    result.result[0].token = createToken(result.result[0])
                    res.send(str_status(1,'登录成功',result.result[0]))                    
                }else{
                    res.send(str_status(0,'用户不存在，请先注册此账号'))
                }
            }else{
                res.send(str_status(0,'验证码已失效'))  
            }
        }else{
            res.send(str_status(0,'验证码错误'))  
        }            
    }
}

module.exports = {
    SendCodeMsg,
    VerifyCodeMsg
}